﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UITrigger : MonoBehaviour 
{
    public List<UITriggerEvent> TriggerEvents = new List<UITriggerEvent>();
    [HideInInspector]
    public  List<UITriggerCondition> TriggerConditions = new List<UITriggerCondition>();
    [HideInInspector]
    public List<UITriggerAction> TriggerActions = new List<UITriggerAction>();

    [HideInInspector]
    public string EventLisp;
    [HideInInspector]
    public string ConditionLisp;
    [HideInInspector]
    public string ActionsLisp;
    public void Awake()
    {
        UITriggerRoot.Inst().AddTrigger(this);
    }
    
    public virtual void OnTrigger(Hashtable argDic)
    {
        if (this.IsEvent(argDic)&&this.IsCondition()) 
        {
            this.RunActions();
        }
    }

    public bool IsEvent(Hashtable argDic) 
    {
        for (int i = 0; i < TriggerEvents.Count; i++)
        {
            if (TriggerEvents[i].EventCondition(argDic))
            {
                return true;
            }
        }
        return false;
    }
    
    public bool IsCondition() 
    {
        for (int i = 0; i < this.TriggerConditions.Count;i++)
        {
            if (!this.TriggerConditions[i].IsCondition()) 
            {
                return false;
            }
        }
        return true;
    }

    void RunActions() 
    {
        for (int i = 0; i < this.TriggerActions.Count;i++)
        {
            this.TriggerActions[i].Exec();
        }
        /*
           trigger
            event
            condition
            actions
         */
    }

}
